const tree = require('./tree');

/**
 * 利用队列的数据结构，每次将所有的下一层子元素放入数组，先进先出。
 * @param  {[BinarySearchTree]} root [二叉树]
 * @return {[array]}      [遍历结果]
 */
const levelOrderTraverseNode = (root) => {
	if (root == null) {
		return root;
	}
	const res = [];
	const children = [];
	children.push(root);
	while (children.length > 0) {
		const current = children.shift();
		res.push(current.val);
		if (current.left) {
			children.push(current.left);
		}
		if (current.right) {
			children.push(current.right);
		}
	}
	return res;
}

const levelOrderTraverseResult = levelOrderTraverseNode(tree);

console.log(levelOrderTraverseResult) // [ 11, 7, 15, 5, 9, 13, 20, 3, 8, 10, 12, 14, 18, 25 ]
